<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class App extends PC_controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Usermodel','m_user');
	}

	public function index(){}

	public function changeUserPassword(){
		$this->validate->setRule('user_password','string');
		$this->validate->setRule('user_password','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['user_password'] = md5($info['user_password'].SALT);
		
		$this->m_user->editUser($info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function addImage(){
		if(!$this->m_app->initFolder()){
			$this->response->setDetail($this->lang->line('error_update'));
			$this->response->responseExit();
		}

		$filename = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : 'a.jpg');
		$path = date('Ym').'/'.date('dHis').rand(10,99).$this->m_app->getCurrentUserId().'.'.pathinfo($filename,PATHINFO_EXTENSION);
		file_put_contents(UPLOAD_PATH_ORIGIN.$path,file_get_contents('php://input'));
		
		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->setAddon(array('url'=>UPLOAD_URL_ORIGIN.$path));
		$this->response->responseExit();
	}

	public function addFile(){
		if(!$this->m_app->initFolder()){
			$this->response->setDetail($this->lang->line('error_update'));
			$this->response->responseExit();
		}

		if(!isset($_SERVER['HTTP_X_FILENAME'])){
			$this->response->setDetail($this->lang->line('error_update'));
			$this->response->responseExit();
		}

		$filename = $_SERVER['HTTP_X_FILENAME'];
		$extern_name = pathinfo($filename,PATHINFO_EXTENSION);

		$content = '';
		if($extern_name == 'pdf'){
			$path = date('Ym').'/'.date('dHis').rand(10,99).$this->m_app->getCurrentUserId().'.pdf';
			file_put_contents(UPLOAD_PATH_FILE.$path,file_get_contents('php://input'));
			if(mime_content_type(UPLOAD_PATH_FILE.$path) != 'application/pdf'){
				$this->response->setDetail($this->lang->line('error_update'));
				$this->response->responseExit();
			}

			$im = new Imagick();
			$im->setResolution(120,120);
			$im->setCompressionQuality(100);
			$im->readImage(UPLOAD_PATH_FILE.$path);
			$filename = pathinfo($path,PATHINFO_FILENAME);
			$content = '<div class=\'mobile-pdf\'>';
			foreach ($im as $key => $record){
				$r_filename = $filename.$key.'.png';
				$record->setImageFormat('png');
				$record->writeImage(UPLOAD_PATH_FILE.$r_filename);
				$content .= '<img src="'.UPLOAD_URL_FILE.$r_filename.'" />';
			}
			$content .= '</div>';
		}elseif($extern_name == 'mp4'){
			$path = date('Ym').'/'.date('dHis').rand(10,99).$this->m_app->getCurrentUserId().'.mp4';
			file_put_contents(UPLOAD_PATH_FILE.$path,file_get_contents('php://input'));
			if(mime_content_type(UPLOAD_PATH_FILE.$path) != 'video/mp4'){
				$this->response->setDetail($this->lang->line('error_update'));
				$this->response->responseExit();
			}
			@exec('ffmpeg2theora '.UPLOAD_PATH_FILE.$path.' > /dev/null &');
			$content = '<video width="100%" height="auto" preload="auto" controls="" autoplay="">
			<source src="'.UPLOAD_URL_FILE.$path.'" type="video/mp4">
			<source src="'.UPLOAD_URL_FILE.(str_replace('.mp4', '.ogv', $path)).'" type="video/ogg"></video>
			<p><a href="'.UPLOAD_URL_FILE.$path.'">下载视频</a></p>';

		}else {
			$path = date('Ym').'/'.str_replace(' ', '', urldecode($filename)).'_'.date('dHis').rand(10,99).$this->m_app->getCurrentUserId().'.'.pathinfo($filename,PATHINFO_EXTENSION);
			file_put_contents(UPLOAD_PATH_FILE.$path,file_get_contents('php://input'));
			$content = '<a target="_blank" href="'.UPLOAD_URL_FILE.$path.'">'.urldecode($filename).'</a>';
		}
		
		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->setAddon(array('content'=>$content));
		$this->response->responseExit();
	}
}
/* End of file app.php */
/* Location: ./application/controllers/app.php */